﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using Shuba.Web;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using Shuba.Model;
using Menu = Shuba.Business.Menu;
using Shuba.Business;

namespace WebAdmin
{
    public partial class WebMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Response.Cache.SetNoStore();

            lblDate.Text = basePage.ProcessingDate().ToString("f");
            Page.Title = ConfigurationManager.AppSettings["title"];

            SetHeader();

            if (!Page.IsPostBack)
            {
                if (Session["UserID"] != null)
                {
                    FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
                    FormsAuthenticationTicket ticket = id.Ticket;

                    string Userdata = ticket.UserData;
                    string[] ArrUserData = Userdata.Split('~');
                    lblName.Text = "NIP: " + ArrUserData[1] + ", NAME: " + ArrUserData[2];

                    CreateMenu();

                    //buat update last activity
                    User user = new User();
                    UserInfo info = new UserInfo { Id = Convert.ToInt32(Session["UserID"]), AppId = ConfigurationManager.AppSettings["app.code"], IsLogin = true, LoginAddr = Request.UserHostAddress.ToString() };
                    user.Update(info);

                    //buat update ticket untuk timeout
                    HttpCookie cookie = FormsAuthentication.GetAuthCookie(HttpContext.Current.User.Identity.Name, false);
                    //FormsAuthenticationTicket ticketEx = FormsAuthentication.Decrypt(cookie.Value);

                    AppParam Appparam = new AppParam();
                    AppParamInfo appParamInfo = Appparam.Get();

                    FormsAuthenticationTicket newticket = new FormsAuthenticationTicket(
                    ticket.Version, ticket.Name, DateTime.Now, DateTime.Now.AddMinutes(appParamInfo.TIME_OUT),
                    ticket.IsPersistent, ticket.UserData, ticket.CookiePath);

                    // add the encrypted ticket to the cookie as data.
                    cookie.Value = FormsAuthentication.Encrypt(newticket);

                    // Update the outgoing cookies collection.
                    Context.Response.Cookies.Set(cookie);
                }
                else
                {
                    Response.Redirect("~/logout.aspx");
                }
            }
        }

        private void SecPol()
        {
            //Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
            //System.Configuration.ConfigurationSection
            ////config.GetSection("customErrors")
            //config.Save();
        }
 
        private void CreateMenu()
        {
            string RootSite = ConfigurationManager.AppSettings["address"].Trim() + "/";
            string header = "";
            string body = "";

            Shuba.Business.Menu menu = new Menu();
            
            //Shuba.Business.User user = new User();
            //Shuba.Model.UserInfo info = user.Get(HttpContext.Current.User.Identity.Name);

            FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
            FormsAuthenticationTicket ticket = id.Ticket;
            string Userdata = ticket.UserData;
            string[] ArrUserData = Userdata.Split('~');

            int UserID = Convert.ToInt32(ArrUserData[0]);
            string AppCode = ConfigurationManager.AppSettings["app.code"];

            List<Shuba.Model.MenuInfo> list, list2;

            list = menu.GetParentMenuList(UserID, AppCode);
            
            foreach (MenuInfo menuInfo in list)
            {
                list2 = menu.GetSubMenuList(menuInfo.Id, UserID, AppCode);
             
                header += "<td><a class=\"mainlevel-trans\" id=\"menu" + menuInfo.Id.ToString() + "\" href=\"#\">" + menuInfo.Name + "</a></td>";
                body += "var tmenu" + menuInfo.Id.ToString() + " = ms.addMenu(document.getElementById(\"menu" + menuInfo.Id.ToString() + "\"));";
                foreach (MenuInfo submenu in list2)
                {
                    body += "tmenu" + menuInfo.Id.ToString() + ".addItem(\" " + submenu.Name + "  \",\"" + RootSite + submenu.Link + "\", 0, 0);";
                }
            }

            litMenuHeader.Text = header;
            litMenu.Text = body;
        }


        private void SetHeader()
        {
            HtmlLink link = new HtmlLink();
            link.Attributes.Add("href", "~/themes/orange/css/main.css");
            link.Attributes.Add("media", "screen");
            link.Attributes.Add("rel", "stylesheet");
            link.Attributes.Add("type", "text/css");
            Page.Header.Controls.Add(link);

            link = new HtmlLink();
            link.Attributes.Add("href", "~/themes/silver/css/main.css");
            link.Attributes.Add("media", "screen");
            link.Attributes.Add("rel", "alternate stylesheet");
            link.Attributes.Add("type", "text/css");
            link.Attributes.Add("title", "silver");
            Page.Header.Controls.Add(link);

            link = new HtmlLink();
            link.Attributes.Add("href", "~/themes/blue/css/main.css");
            link.Attributes.Add("media", "screen");
            link.Attributes.Add("rel", "alternate stylesheet");
            link.Attributes.Add("type", "text/css");
            link.Attributes.Add("title", "blue");
            Page.Header.Controls.Add(link);

            link = new HtmlLink();
            link.Attributes.Add("href", "~/themes/red/css/main.css");
            link.Attributes.Add("media", "screen");
            link.Attributes.Add("rel", "alternate stylesheet");
            link.Attributes.Add("type", "text/css");
            link.Attributes.Add("title", "red");
            Page.Header.Controls.Add(link);

            link = new HtmlLink();
            link.Attributes.Add("href", "~/themes/green/css/main.css");
            link.Attributes.Add("media", "screen");
            link.Attributes.Add("rel", "alternate stylesheet");
            link.Attributes.Add("type", "text/css");
            link.Attributes.Add("title", "green");
            Page.Header.Controls.Add(link);

            link = new HtmlLink();
            link.Attributes.Add("href", "~/themes/orange/css/main.css");
            link.Attributes.Add("media", "screen");
            link.Attributes.Add("rel", "alternate stylesheet");
            link.Attributes.Add("type", "text/css");
            link.Attributes.Add("title", "orange");
            Page.Header.Controls.Add(link);

            HtmlGenericControl js = new HtmlGenericControl("script");

            js = new HtmlGenericControl("script");
            js.Attributes["type"] = "text/javascript";
            js.Attributes["src"] = ResolveUrl("~/transmenu.js");
            Page.Header.Controls.Add(js);

            js = new HtmlGenericControl("script");
            js.Attributes["type"] = "text/javascript";
            js.Attributes["src"] = ResolveUrl("~/styleswitch.js");
            Page.Header.Controls.Add(js);

            link = new HtmlLink();
            link.Attributes.Add("href", "~/calendar/calendar-white.css");
            link.Attributes.Add("media", "all");
            link.Attributes.Add("rel", "stylesheet");
            link.Attributes.Add("type", "text/css");
            link.Attributes.Add("title", "calendar-white");
            Page.Header.Controls.Add(link);
        }
    }
 }